<script setup>
import { ref, computed } from 'vue'
import { useUserStore } from '@/stores/userStore'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const user = computed(() => userStore.user || {})
const username = ref(user.value?.name || '')
const previewAvatar = ref(null)

function handleAvatarChange(file) {
  const reader = new FileReader()
  reader.onload = (e) => {
    previewAvatar.value = e.target.result
  }
  reader.readAsDataURL(file.raw)
}

import http from '@/utils/http'

console.log('userStore.id:', userStore.id)
console.log('userStore.avatar:', userStore.avatar)

async function saveChanges() {
  try {
    const res = await http.post('auth/update_profile/', {
      user_id: userStore.id,
      username: username.value,
      avatar: previewAvatar.value || userStore.avatar

    })
    console.log('提交数据', {
      user_id: userStore.id,
      username: username.value,
      avatar: previewAvatar.value
    })

    userStore.setUser({
      user_id: userStore.id,
      username: res.data.username,
      avatar: res.data.avatar,
      role: userStore.role
    })
    console.log("userStore", userStore)

    ElMessage.success('修改成功！')
  } catch (err) {
    ElMessage.error(err.response?.data?.error || '保存失败')
  }
}

</script>

<template>
  <div class="profile-page">
    <!-- 返回按钮 -->
    <div class="profile-header">
      <el-button link @click="$router.back()" class="back-btn">
        <el-icon>
          <ArrowLeft />
        </el-icon>
        <span>返回</span>
      </el-button>
        <h2>个人中心</h2>
      
    </div>

    <el-form label-position="top" class="profile-form">
      <el-form-item label="头像">
        <el-upload class="avatar-uploader" :show-file-list="false" :on-change="handleAvatarChange" :auto-upload="false">
          <el-avatar :size="80" :src="previewAvatar || user.avatar || '/mock-bg.jpg'" />
        </el-upload>
      </el-form-item>

      <el-form-item label="昵称">
        <el-input v-model="username" placeholder="请输入昵称" />
      </el-form-item>

      <!-- <el-form-item label="身份角色">
        <el-tag type="success">{{ user.role }}</el-tag>
      </el-form-item> -->

      <el-button type="primary" @click="saveChanges">保存修改</el-button>
    </el-form>
  </div>
</template>


<style scoped>
.profile-page {
  min-height: 100vh;
  background: linear-gradient(to bottom right, #fdf4e3, #f7e6d0);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 60px 20px;
}


.profile-form {
  background: #fffdf9;
  padding: 48px 36px;
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.08);
  max-width: 480px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 28px;
  font-family: 'Segoe UI', sans-serif;
}

.profile-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 480px;
  margin-bottom: 12px;
}

.profile-header h2 {
  font-size: 26px;
  font-weight: 700;
  color: #4a2e18;
  flex: 1;
  /* text-align: center; */
}


.back-btn {
  font-size: 14px;
  color: #8b5e3c;
  margin-right: 20px;
}

.el-form-item--label-top {
  text-align: center;
}

.el-form-item__label {
  font-size: 15px;
  font-weight: 500;
  color: #4a2e18;
  margin-bottom: 6px;
}

.avatar-uploader {
  display: flex;
  justify-content: center;
  cursor: pointer;
}

.el-avatar {
  border: 3px solid #c19a6b;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.el-input__inner {
  font-size: 15px;
}

.el-button--primary {
  background-color: #c19a6b;
  border-color: #c19a6b;
  font-size: 16px;
  font-weight: 600;
  padding: 12px 0;
}

.el-button--primary:hover {
  background-color: #b88a5c;
  border-color: #b88a5c;
}
</style>
